//
//  SYTListViewController.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/8/7.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "SYTListViewController.h"
#import "AppDelegate.h"
#import "ChatClassModel.h"
#import "ChatListTableViewCell.h"
#import "ChatUserModel.h"
#import "RYChatViewController.h"
#import "RYChatBaseView.h"
#import "RYChatBaseViewController.h"

@interface SYTListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    RYChatBaseView *baseView;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,strong)  NSMutableArray *dataArr;

@end

@implementation SYTListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.dataArr = [NSMutableArray array];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self.dataArr addObjectsFromArray:appDelegate.AllArray];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"ChatListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChatListTableViewCell"];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ChatClassModel *model = self.dataArr[section];

    if ([model.isShow isEqualToString:@"0"]) {
        return 0;
    }else{
        return model.ListStudent.count;

    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatClassModel *model = self.dataArr[indexPath.section];
    ChatUserModel *userModel = model.ListStudent[indexPath.row];
    ChatListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatListTableViewCell"];
    cell.nameLable.text = userModel.name;
    cell.classLable.text = model.className;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ChatClassModel *model = self.dataArr[section];

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#E4E4E4"];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, headerView.bounds.size.width-20, 20)];
    lable.text = model.className;
    lable.font = [UIFont systemFontOfSize:15];
    lable.textColor = [UIColor colorWithHexString:@"#333333"];
    [headerView addSubview:lable];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
    btn.tag = 100 + section;
    [btn addTarget:self action:@selector(showCellWithChangeModel:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:btn];
    return headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ChatClassModel *model = self.dataArr[indexPath.section];
    ChatUserModel *userModel = model.ListStudent[indexPath.row];
    if (userModel.imid.length <= 0 ) {
        return;
    }
    if (self.chooseChatManWithTagerId) {
        self.chooseChatManWithTagerId(userModel.imid,userModel.name);
    }
}
-(void)showCellWithChangeModel:(UIButton *)sender{
    NSInteger index = sender.tag -100;
    ChatClassModel *model = self.dataArr[index];
    NSLog(@"%ld",[model.isShow integerValue]);

    if ([model.isShow isEqualToString:@"0"]) {
        model.isShow = @"1";
    }else{
        model.isShow = @"0";
    }
    [self.tableView reloadData];
}
@end
